package nccu.converter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;
import java.util.Calendar;

public class AccountValueConverter implements Converter {

    private Log log = LogFactory.getLog(AccountValueConverter.class);

    public void marshal(Object source, HierarchicalStreamWriter writer,
            MarshallingContext context) {
        BigDecimal value = (BigDecimal) source;
        writer.setValue(String.valueOf(value));
    }

    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext context) {
        String obj = reader.getValue();
        if (obj == null) {
            return null;
        }
        try {
            BigDecimal value = new BigDecimal(obj);
        } catch (NumberFormatException e) {
            log.warn("could not parse decimal", e);
        }

        return obj;
    }

    public boolean canConvert(Class type) {
        return (type.equals(BigDecimal.class) || type.equals(String.class));
    }
}
